<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.text.ParseException"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.ucap.base.model.wcm.Manuscript,com.ucap.utils.*" %>
<%@page import="com.ucap.system.db.DataBaseFactory"%>
<%@include file="/classes/DBHelper.class.jsp" %>
<%
try{
	request.setCharacterEncoding("utf-8");
	final String channelId = "215856b2cd3046b7ada0b9b018568d9d";//临时栏目ID
	//
	String syh = "";//生成规则
	final String website = "9bbbb07c1d62450297ca9cfef795e523";
	String import_opt_user_id ="9999999999999999";// "administrator";
	String title = getParameter(request,"title");//非空
	String content = getParameter(request,"content");//非空
	String time = getParameter(request,"time");//非空
	String wh = getParameter(request,"wh");//rs.getString("wh");
// 	String syh = getParameter(request,"syh");
	String nrgs = getParameter(request,"nrgs");//rs.getString("nrgs");
	String zrz = getParameter(request,"zrz");//rs.getString("zrz");
	String jgcode = getParameter(request,"jgcode");//rs.getString("zrz");
	if(nullOrEmpty(title) || nullOrEmpty(content) || nullOrEmpty(time) || nullOrEmpty(wh) || nullOrEmpty(zrz)){
	    out.print("失败");
	    return;
	}
	long put_time = TimeUtil.dateStrToGMTLong(time,"GMT+8:00");		//toDate("yyyy-MM-dd HH:mm:ss",time).getTime();
    boolean flag = false;
    boolean result = false;
    
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    
    String memo = "摘要";
    /*
     * 新建测试文章类，并设置参数
     * 
     */
    Manuscript manuscript = new Manuscript();
    //获取文章Id
    String infoId = generateString32();
    manuscript.setManuscriptId(infoId);
    manuscript.setChannelId(channelId);
    manuscript.setTitle(title);// 去掉title空格
    manuscript.setType("0");// 这里的type为1时可能出现稿件生成静态页正文出不来，显示为图片
    manuscript.setStatus("1");// 这里的type直接为3（已签发）时可能出现稿件静态页不生成。
    //manuscript.setKeyword("关键字");
    manuscript.setKeyword("");
    
// 	 String sql = "insert into cms_manuscript_metadata_g values('"
// 	         + infoId + "','" + syh + "','','','','" + title + "','"
// 	         + wh + "','" + nrgs + "'," + put_time + ",'" + zrz
// 	         + "'," + put_time + ",'','','','','','','','','','')";
	if(nullOrEmpty(jgcode)){
	    jgcode = "014051247";
	}
	//查询索引号
	if(!nullOrEmpty(jgcode)){
		String sqlSyh = "select max(g_syh) g_syh from cms_manuscript_metadata_g t where t.g_syh like ? and t.manuscript_metadata_id <> 'a89eda935ac74e24a47a2783c4adb1e9'";
		Map<String,Object> m = getObject(sqlSyh,new Object[]{jgcode+'%'});
		if(null!=m){
		    String str = (String)m.get("g_syh");
		    syh = syh(str);
		}
		if(nullOrEmpty(syh)){
		    syh = "014051247/"+fotmatTime(new Date(),"yyyy")+"-00001";
		}
	}
	String sql = "insert into CMS_MANUSCRIPT_METADATA_G (MANUSCRIPT_METADATA_ID,G_SYH,G_TM1,G_WH1,G_NRGS,G_CWRQ,G_ZRZ,G_FBRQ) "+
			"values (?,?,?,?,?,?,?,?)";
	executeUpdate(sql,new Object[]{infoId,syh,title,wh,nrgs,put_time,zrz,put_time});
	
	manuscript.setCreatedTime(put_time);
    manuscript.setPublishedTime(put_time);
    manuscript.setIsDisplayContent("0");
    manuscript.setIsSticky("0");
    manuscript.setCreatorId(import_opt_user_id);

    manuscript.setIsModifiedSeqNum("0");
    manuscript.setSortedTime(0l);
    manuscript.setIsEncrypted("0");
    manuscript.setIsPageBreak("0");
    manuscript.setSeqNum(System.currentTimeMillis());
    manuscript.setModifiedTime(System.currentTimeMillis());
    manuscript.setMemo(memo);
    manuscript.setUrl(sf.format(new Date(put_time)));
    if (!manuscript.getUrl().endsWith(".shtml")) {// 新增稿件
        manuscript.setUrl(manuscript.getUrl() + "/"
                + manuscript.getManuscriptId() + ".shtml");// 生成稿件URL
    }
    com.ucap.migration.manuscript.ManuscriptService manuscriptService = new com.ucap.migration.manuscript.ManuscriptService();
    com.ucap.migration.resource.ResourceTextHandle resourceTextHandle = new com.ucap.migration.resource.ResourceTextHandle();
    try {
        //写入目的数据库
        resourceTextHandle.saveText(manuscript, content);
        result = manuscriptService.saveManuscript(manuscript);
        if(result){
            out.print("成功");
        }else{
            out.print("失败");
        }
    } catch (Exception e) {
        e.printStackTrace();
        out.print(e.getMessage());
        out.print("失败");
    }	

    
}catch(Exception e){
    e.printStackTrace();
     out.print(e.getMessage());
    out.print("失败");
}
%>


<%!private String getParameter(HttpServletRequest request, String name) {
        String val = request.getParameter(name);
        if (null != val && !"".equals(val)) {
            return val;
        }
        return "";
    }

    private boolean nullOrEmpty(String str) {
        if (null != str && !"".equals(str)) {
            return false;
        }
        return true;
    }

    private Date toDate(String format, String strDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(strDate);
    }

    private String fotmatTime(Date d, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(d);
    }

    private String generateString32() {
        String radStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuffer generateRandStr = new StringBuffer();
        Random rand = new Random();
        int length = 32;
        for (int i = 0; i < length; i++) {
            int randNum = rand.nextInt(36);
            generateRandStr.append(radStr.substring(randNum, randNum + 1));
        }
        return generateRandStr + "";
    }
    
    private String syh(String syh){
        if(!nullOrEmpty(syh)){
	         int index = syh.lastIndexOf("-");
	         int index2 = syh.lastIndexOf("/");
	         if(index!=-1){
	             //截取年份
	             String year = syh.substring(index2+1,index);
	             //获取当前年份
	             String currYear = fotmatTime(new Date(),"yyyy");
	             if(currYear.equals(year)){
	                 String num = syh.substring(index+1);
		             String str3 = syh.substring(0,index+1);
		             String str4 = String.format("%05d", Integer.valueOf(num)+1); 
		             return str3+str4;
	             }else{
	                 String jg = syh.substring(0, index2+1);
	                 return jg+currYear+"-00001";
	             }
	         }
        }
        return null;
    }%>